/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ZoneConnectionsPropertyEditorPanel.java
 *
 * Created on Aug 13, 2011, 12:29:16 AM
 */
package pt.utl.ist.isr.livetrack.ui.propedit;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import org.openide.explorer.propertysheet.PropertyEnv;
import org.openide.util.Exceptions;
import pt.utl.ist.isr.livetrack.analysis.ImageUtils;
import pt.utl.ist.isr.livetrack.server.LTServer;
import pt.utl.ist.isr.livetrack.server.interfaces.RoiType;
import pt.utl.ist.isr.livetrack.ui.CameraNode;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class CameraROIPropertyEditorPanel extends javax.swing.JPanel {

    private final CameraNode cameraNode;
    private final LTServer server;
    private boolean updating = false;
    private boolean valid;
    private int topLX, topLY, bottomRX, bottomRY;
    private ImageIcon icon = new ImageIcon();
    private BufferedImage img;

    public CameraROIPropertyEditorPanel(PropertyEnv pe) {
        initComponents();

        cameraNode = (CameraNode) pe.getBeans()[0];
        this.server = cameraNode.getServer();

        roiTypeComboBox.setModel(new DefaultComboBoxModel(RoiType.values()));
        roiTypeComboBox.setSelectedItem(cameraNode.getCamera().getRoiType());

        byte[] byteArray = cameraNode.getCamera().getImageAsByteArray();
        if (byteArray != null) {
            try {
                valid = false;
                img = ImageIO.read(new ByteArrayInputStream(byteArray));
                imageLabel.setIcon(icon);
                icon.setImage(img);
                imageLabel.setMaximumSize(new Dimension(img.getWidth(), img.getHeight()));
                valid = true;
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        } else {
            imageLabel.setText("No image - please acquire an image with the camera first.");
        }

        Rectangle2D roi = cameraNode.getCamera().getRoi();
        topLX = (int) roi.getX();
        topLY = (int) roi.getY();
        bottomRX = topLX + (int) roi.getWidth();
        bottomRY = topLY + (int) roi.getHeight();

        updateIcon();
        repaint();
        validateTree();
    }

    private JLabel createImageLabel() {
        return new JLabel();
    }

    private void updateIcon() {
        if (img != null) {
            BufferedImage i = ImageUtils.deepCopy(img);
            Graphics g = i.getGraphics();
            switch ((RoiType) roiTypeComboBox.getSelectedItem()) {
                case ACCEPT:
                    g.setColor(new Color(0f, 1f, 0f, 0.5f));
                    break;
                case REJECT:
                    g.setColor(new Color(1f, 0f, 0f, 0.5f));
                    break;
                case DISABLED:
                    g.setColor(new Color(0.5f, 0.5f, 0.5f, 0.5f));
                    break;
            }
            g.fillRect(topLX, topLY, bottomRX - topLX, bottomRY - topLY);
            icon.setImage(i);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        imageLabel = createImageLabel();
        jLabel1 = new javax.swing.JLabel();
        roiTypeComboBox = new javax.swing.JComboBox();

        imageLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        imageLabel.setText(org.openide.util.NbBundle.getMessage(CameraROIPropertyEditorPanel.class, "CameraROIPropertyEditorPanel.imageLabel.text")); // NOI18N
        imageLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        imageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                imageLabelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                imageLabelMouseReleased(evt);
            }
        });
        imageLabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                imageLabelMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                imageLabelMouseMoved(evt);
            }
        });
        jScrollPane1.setViewportView(imageLabel);

        jLabel1.setText(org.openide.util.NbBundle.getMessage(CameraROIPropertyEditorPanel.class, "CameraROIPropertyEditorPanel.jLabel1.text")); // NOI18N

        roiTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        roiTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                roiTypeComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(roiTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(roiTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void imageLabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageLabelMousePressed
        if (valid) {
            Point p = imageLabel.getMousePosition(true);
            topLX = bottomRX = p.x;
            topLY = bottomRY = p.y;
            updating = true;
        }
    }//GEN-LAST:event_imageLabelMousePressed

    private void imageLabelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageLabelMouseReleased
        if (valid) {
            Point p = imageLabel.getMousePosition(true);
            if (p != null) {
                bottomRX = Math.min(p.x, img.getWidth());
                bottomRY = Math.min(p.y, img.getHeight());
            }
            cameraNode.getCamera().setRoi(new Rectangle(topLX, topLY, bottomRX - topLX, bottomRY - topLY));
            updating = false;
        }
    }//GEN-LAST:event_imageLabelMouseReleased

    private void imageLabelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageLabelMouseMoved
    }//GEN-LAST:event_imageLabelMouseMoved

    private void imageLabelMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageLabelMouseDragged
        if (valid) {
            if (updating) {
                Point p = imageLabel.getMousePosition(true);
                if (p != null) {
                    bottomRX = Math.min(p.x, img.getWidth());
                    bottomRY = Math.min(p.y, img.getHeight());
                    updateIcon();
                    repaint();
                    validateTree();
                }
            }
        }
    }//GEN-LAST:event_imageLabelMouseDragged

    private void roiTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_roiTypeComboBoxActionPerformed
        cameraNode.getCamera().setRoiType((RoiType) roiTypeComboBox.getSelectedItem());
        updateIcon();
        repaint();
        validateTree();
    }//GEN-LAST:event_roiTypeComboBoxActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox roiTypeComboBox;
    // End of variables declaration//GEN-END:variables
}
